//
//  typeTableViewController.m
//  AIM
//
//  Created by user10 on 2017/5/19.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "typeTableViewController.h"
#import "typeTableViewCell.h"
#import "NetWorkTools.h"
@interface typeTableViewController ()

@property (nonatomic,strong)NSArray *data;

@property (nonatomic, strong) NSString *typeRsultStr;

@property (nonatomic, strong) NSArray *resultArray;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) NSMutableArray *keysArray;

@property (nonatomic, strong) NSMutableArray *objectArray;

@end

@implementation typeTableViewController

- (NSArray *)resultArray
{
    if (!_resultArray) {
        _resultArray = [NSMutableArray array];
    }
    return _resultArray;
}

- (NSMutableDictionary *)dataDic {
    if (!_dataDic) {
        _dataDic = [NSMutableDictionary dictionary];
    }
    return _dataDic;
}

- (NSMutableArray *)keysArray {
    
    if (!_keysArray) {
        _keysArray = [NSMutableArray array];
        
    }
    return _keysArray;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    NSString *webServiceStr = [NSString stringWithFormat:
                               @"<web:getPostCategoryData>\
                               <arg0>%@</arg0>\
                               </web:getPostCategoryData>",COMPANY];
    //初始化网络工具类
    NetWorkTools *tools = [NetWorkTools shareTools];
    //发送网络请求
    [tools request:POST urlString:[NSString stringWithFormat:@"http://%@:8888/webservice/ws/PostCategoryImplPort?wsdl",[NSUD objectForKey:@"URL"]] parametersStr:webServiceStr finished:^(id responseObject, NSError *error) {
        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *string = result;
        NSRange startRange = [string rangeOfString:@"@@"];
        NSRange endRange = [string rangeOfString:@"</return>"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        if (range.location != NSNotFound) {
            NSString *resultString = [string substringWithRange:range];
            NSData * getJsonData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];
            _resultArray= [getDict objectForKey:@"DATA"];
            NSMutableArray *mgroupsArray = [NSMutableArray array];
            for (NSDictionary *tagDict in _resultArray) {
                typeModel *typeResult = [typeModel typeWithDictionary:tagDict];
                [mgroupsArray addObject:typeResult];
            }
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            for (typeModel *typeResult in mgroupsArray) {
                if ([typeResult.MSG_CATEGORY_CLASS integerValue] == 0) {
                    NSMutableArray *objectArray =  [dic objectForKey:[NSNumber numberWithInteger:[typeResult.MSG_CATEGORY_CD integerValue]]];
                    
                    if (!objectArray) {
                        [dic setObject:[NSMutableArray array] forKey:[NSNumber numberWithInteger:[typeResult.MSG_CATEGORY_CD integerValue]]];
                        [self.keysArray addObject:typeResult];
                    } else {
                        [objectArray addObject:typeResult];
                    }
                }else {
                    NSMutableArray *objectArray =  [dic objectForKey:[NSNumber numberWithInteger:[typeResult.MSG_CATEGORY_CD integerValue]/10]];
                    if (!objectArray) {
                        [dic setObject:[NSMutableArray array] forKey:[NSNumber numberWithInteger:[typeResult.MSG_CATEGORY_CD integerValue]/10]];
                    } else {
                        [objectArray addObject:typeResult];
                    }
                }
            }
            self.dataDic = dic;
            dispatch_async(dispatch_get_main_queue(), ^{
                for (int i = 0; i <self.keysArray.count; i++) {
                    typeModel *model =  self.keysArray[i];
                    NSArray *array = [self.dataDic objectForKey: [NSNumber numberWithInteger:[model.MSG_CATEGORY_CD integerValue]]];
                    for (int j = 0; j < array.count; j++) {
                        typeModel *cellModel = array[j];
                        if ([cellModel.MSG_CATEGORY_CD isEqualToString:_histroyStr]) {
                            NSLog(@"%d---%d",i,j);
                            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:j inSection:i];
                            [self tableView:self.tableView didSelectRowAtIndexPath:indexpath];
                        }
                    }
                }
            });
            
            [self.tableView reloadData];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Network error" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            alert.delegate = self;
            [alert show];
        }
        
        
    }];
    
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    typeModel *model =  self.keysArray[section];
    
    return   model.MSG_CATEGORY_NM ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.keysArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    typeModel *model =  self.keysArray[section];
    
    NSArray *array = [self.dataDic objectForKey: [NSNumber numberWithInteger:[model.MSG_CATEGORY_CD integerValue]]];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"friendCell";
    
    typeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (nil == cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"typeTableViewCell"  owner:self options:nil] lastObject];
    }
    typeModel *model =  self.keysArray[indexPath.section];
    
    NSArray *array = [self.dataDic objectForKey: [NSNumber numberWithInteger:[model.MSG_CATEGORY_CD integerValue]]];
    
    
    typeModel *cellModel = array[indexPath.row];
    
    cell.textLabel.text = cellModel.MSG_CATEGORY_NM;
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor magentaColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    typeModel *model =  self.keysArray[indexPath.section];
    NSArray *array = [self.dataDic objectForKey: [NSNumber numberWithInteger:[model.MSG_CATEGORY_CD integerValue]]];
    typeModel *cellModel = array[indexPath.row];
    
    //之前选中的cell
    typeTableViewCell *lastCell = [tableView viewWithTag:1];
    lastCell.image.hidden = YES;
    lastCell.tag = 0;
    lastCell.backgroundColor = [UIColor whiteColor];
    
    //目前选中的cell
    typeTableViewCell *nowCell = [tableView cellForRowAtIndexPath:indexPath];
    nowCell.image.hidden = NO;
    nowCell.tag = 1;
    nowCell.backgroundColor = [UIColor magentaColor];
    
    _typeRsultStr = cellModel.MSG_CATEGORY_CD;
    
    
    if ([self.typeDelegate respondsToSelector:@selector(typeValuesWithStr:)]) {
        // 调用代理对象的刷新方法，代理对象去实现方法
        [self.typeDelegate typeValuesWithStr:_typeRsultStr];
    }
}



@end
